layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //用户列表
    var tableIns = table.render({
        elem: '#userList',
        url : '/manage/user/data',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "userListTable",
        cols : [[
            {field: 'account', title: '帐号',align:"left"},
            {field: 'nickname', title: '用户名',align:"left"},
            {field: 'name', title: '姓名',align:"left"},
            {field: 'email', title: '用户邮箱',align:'left',templet:function(d){
                return '<a class="layui-blue" href="mailto:'+d.email+'">'+d.email+'</a>';
            }},
            {field: 'sex', title: '性别',  align:'center',templet:function(d){
                    return d.sex == "F" ? "♀" : "♂";
                }},
            {field: 'status', title: '用户状态',  align:'center',templet:function(d){
                return d.status == "0" ? "正常" : "锁定";
            }},
            {field: 'lastLoginTime', title: '最后登录时间', align:'left'},
            {title: '操作', minWidth:175, templet:'#userListBar',fixed:"right",align:"center"}
        ]]
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            table.reload("newsListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //查看用户
    function view(data){









        var index = layui.layer.open({
            title : "查看用户",
            type : 2,
            content : "/manage/user/update?id=" + data.id,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                setTimeout(function(){
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        window.sessionStorage.setItem("index",index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(window.sessionStorage.getItem("index"));
        })






       /*

        var index = layui.layer.open({
            title: "查看用户",
            type: 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area: ["550px", "350px"],
            content: "/manage/user/update?id=" + data.id,
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
            }
        })*/
    }


    //列表操作
    table.on('tool(userList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'view'){ //编辑
            view(data);
        }else if(layEvent === 'userenable'){
            //启用
            if(data.status == '1'){
                layer.confirm('是否确定解锁该用户？',{icon:3, title:'提示信息'},function(index){
                    $.post("/manage/user/status",{
                        account : data.account,
                        status : '0'
                    },function(data){
                        data = JSON.parse(data)
                        if(data.flag ==="success"){
                            layer.msg("操作成功",{icon: 1});
                            tableIns.reload();
                            layer.close(index);
                        }else{
                            layer.msg("操作失败！", {icon: 2});
                        }
                    })
                });
            }else{
                layer.msg("该用户状态为【正常】，无法操作！",{icon: 5});
            }
        }else if(layEvent === 'userdisable'){
            //锁定
            if(data.status == '0'){
                layer.confirm('是否确定锁定该用户？',{icon:3, title:'提示信息'},function(index){
                    $.post("/manage/user/status",{
                        account : data.account,
                        status : '1'
                    },function(data){
                        data = JSON.parse(data)
                        if(data.flag ==="success"){
                            layer.msg("操作成功",{icon: 1});
                            tableIns.reload();
                            layer.close(index);
                        }else{
                            layer.msg("操作失败！", {icon: 2});
                        }
                    })
                });
            }else{
                layer.msg("该用户状态为【锁定】，无法操作！",{icon: 5});
            }
        }
    });

})
